{extend name="index/iframe" /}

{block name="content"}
<form class="layui-form layui-form-my" action="">
    <div class="layui-form-item">
        <label class="layui-form-label">父级分类</label>
        <div class="layui-input-block">
            <select name="pid" id="pid" lay-filter="pid" lay-search="">
                {volist name="$faq_list" id="vo"}
                <option value="{$vo.id}-{$vo.rid}">{:str_repeat($vo['html'], $vo['rid'])}{$vo.title}</option>
                {/volist}
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">分类标题</label>
        <div class="layui-input-block">
            <input type="text" name="title" id="title" placeholder="请输入标题" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">提问</label>
        <div class="layui-input-block">
            <input type="text" name="question" id="question" placeholder="请输入提问" autocomplete="off" class="layui-input layui-disabled" disabled>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">答案</label>
        <div class="layui-input-block">
            <textarea name="answer" id="answer" placeholder="请输入答案" class="layui-textarea layui-disabled" disabled></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn my-btn" lay-submit="" lay-filter="submitBtn">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
{/block}

{block name="css"}
{/block}

{block name="js"}
<script>
    layui.use(['form'], function(){
        var form = layui.form
            ,$ = layui.$
            ,layer = layui.layer;

        form.on('select(pid)', function(data){
            var d = data.value
                ,rid = d.split("-");
            if(rid[1]==2){ //验证等级
                $('#title').addClass('layui-disabled').attr('disabled', true);
                $('#question').removeClass('layui-disabled').attr('disabled', false);
                $('#answer').removeClass('layui-disabled').attr('disabled', false);
            }else{
                $('#title').removeClass('layui-disabled').attr('disabled', false);
                $('#question').addClass('layui-disabled').attr('disabled', true);
                $('#answer').addClass('layui-disabled').attr('disabled', true);
            }
            form.render();
        });

        //提交
        form.on('submit(submitBtn)', function(data){
            var load = layer.load()
                ,d = data.field.pid
                ,id = d.split("-");
            data.field.pid = id[0];
            data.field.rid = id[1];

            $.post('{:url("Faq/add")}', data.field, function(json){
                layer.close(load);
                if(json.code==0){
                    layer.msg(json.msg, {icon: 1, time: 1000});
                    $('#pid').html(json.data);
                    form.render();
                }else{
                    layer.msg(json.msg, {icon: 0, time: 1000});
                }
            }, 'JSON').error(function(){
                layer.close(load);
                layer.msg('连接超时，请联系后台管理员', {icon: 2, time:3000});
            });
            return false;
        });
    });
</script>
{/block}